package org.mcflier.appuntamento.tasks;

import java.sql.Timestamp;
import java.util.List;
import java.util.Set;

import org.mcflier.appuntamento.core.Factory;
import org.mcflier.appuntamento.core.exception.ModulesException;
import org.mcflier.appuntamento.data.model.TorrentList;
import org.mcflier.appuntamento.io.driver.db.JDBCDriver;
import org.mcflier.appuntamento.io.driver.rss.torrent.AbstractRssTorrentDriver;
import org.mcflier.appuntamento.io.driver.rss.torrent.TorrentAggregator;
import org.mcflier.appuntamento.io.exception.RSSConnectionException;
import org.mcflier.appuntamento.io.exception.RSSParsingException;
import org.mcflier.appuntamento.tasks.exception.CreatingTaskException;

public class NewTorrentsTask extends AbstractTask {
        
        private Factory factory;
        private JDBCDriver jdbc;
        private TorrentAggregator torrents;
        private Timestamp lastExecution;

        public NewTorrentsTask(String name, long delay) throws CreatingTaskException, ModulesException {
                super(name, delay);
                this.factory = Factory.getInstance();
                try {
                        this.jdbc = (JDBCDriver)this.factory.getModuleInstance(Factory.MODULE_JDBC);
                        this.torrents = (TorrentAggregator)this.factory.getModuleInstance(Factory.MODULE_RSS_TORRENT);
                        
                        this.lastExecution = new Timestamp(0);
                } catch (ModulesException e) {
                        throw new CreatingTaskException(this.getClass().getSimpleName()+": Error loading the necessary modules", e);
                }
        }

        @Override
        public void doSomething() {
                this.logger.info("Executing task: "+this.getClass().getSimpleName());
                AbstractRssTorrentDriver driver = null;
                List<TorrentList> list = null;
                
                try {
                        Set<String> keys = this.torrents.getDrivers().keySet();
                        
                        for (String key : keys) {
                                driver = this.torrents.getDriver(key);
                                
                                this.logger.info("Retrieving HD torrents from: "+key);
                                list = driver.getHDTorrents(this.lastExecution);
                                this.logger.info("Storing HD torrents retrieved from :"+key);
                                for(TorrentList torrentlist : list){
                                        this.jdbc.addTorrents(torrentlist);
                                }
                                
                                this.logger.info("Retrieving SD torrents from: "+key);
                                list = driver.getSDTorrents(this.lastExecution);
                                this.logger.info("Storing SD torrents retrieved from :"+key);
                                for(TorrentList torrentlist : list){
                                        this.jdbc.addTorrents(torrentlist);
                                }
                                
                                this.logger.info("Retrieving undefined torrents from: "+key);
                                list = driver.getUndefinedTorrents(this.lastExecution);
                                this.logger.info("Storing undefined torrents retrieved from :"+key);
                                for(TorrentList torrentlist : list){
                                        this.jdbc.addTorrents(torrentlist);
                                }
                        }
                } catch (RSSConnectionException e) {
                        this.logger.warning("Error retrieving torrents from driver '"+((driver!=null)?driver.getSource().getName():"unknownn driver")+"'.\n"+e);
                } catch (RSSParsingException e) {
                        this.logger.warning("Error retrieving torrents from driver '"+((driver!=null)?driver.getSource().getName():"unknownn driver")+"'.\n"+e);
                }
        }
}